﻿//JQueryDatepicker
(function ($) {

    var methods = {
        init: function (options) {
            var settings = {
                Value: null,
                ButtonImageUrl: '/Images/calendar.png'
            };

            // plugin code here

            var $this = jQuery(this);
            var data = $this.data('JQueryDatepicker');

            // If the plugin hasn't been initialized yet
            if (!data) {

                // If options exist, lets merge them
                // with our default settings
                if (options) {
                    jQuery.extend(settings, options);
                }

                //Working class declaration
                function JQueryDatepicker(options) {
                    //Reference to JS object
                    var that = this;
                    //Reference to DOM container
                    var $this = options.$this;

                    //Control initialization
                    this.Initialize = function () {
                        var $value = $this.find('.Value');

                        var parsedDate = String.GetDateFromString(options.Value);
                        if (parsedDate) {
                            $value.val(options.Value);
                        }

                        $value.datepicker(
                        {
                            changeMonth: true,
                            changeYear: true,
                            yearRange: '1900:2099',
                            constrainInput: true,
                            showOn: 'button',
                            buttonImageOnly: true,
                            buttonImage: options.ButtonImageUrl
                        }); //.mask('99.99.9999');

                        $value.change(function () {
                            if (!Date.ValidateDate($value.val())) {
                                $value.val('');
                                $value.addClass('Error');
                            }
                            else {
                                $value.removeClass('Error');
                            }
                        });

                        $value.focusin(function () {
                            $value.removeClass('Error');
                        });
                    };
                }

                //Add $this parameter to settings to provide easy way to get link to DOM
                settings.$this = jQuery(this);
                data = new JQueryDatepicker(settings);
            }
            jQuery(this).data('JQueryDatepicker', data);
            return data;
        }
    };

    $.fn.JQueryDatepicker = function (method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            jQuery.error('Method ' + method + ' does not exist on jQuery.JQueryDatepicker');
        }

    };

})(jQuery);

